package com.yanqu.road.server.manager.activity.commonteam.pb;

import com.yanqu.road.entity.activity.commonteam.data.CommonTeamApplyData;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeam;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;

import java.util.List;

public class CommonTeamPb {

    /**
     * 构建 CommonTeamData
     * @param team
     * @param showMemberFlag
     * @param showApplyFlag
     * @return
     */
    public static CommonTeamProto.CommonTeamData.Builder buildCommonTeamData(CommonTeam team, boolean showMemberFlag, boolean showApplyFlag) {
        if (team == null) {
            return null;
        }
        CommonTeamProto.CommonTeamData.Builder builder = CommonTeamProto.CommonTeamData.newBuilder();
        builder.setActivityId(team.getActivityId());
        builder.setTeamId(team.getTeamId());
        builder.setServerId(team.getServerId());
        builder.setTeamName(team.getTeamName());
        builder.setLeaderId(team.getLeaderId());
        builder.setLeaderName(team.getLeaderName());
        builder.setScore(team.getTeamPower());
        builder.setCreateTime(team.getCreateTime());
        if (showMemberFlag) {
            for (CommonTeamUserData userData : team.getMemberDataMap().values()) {
                builder.addMember(buildCommonTeamUserData(userData));
            }
        }
        if (showApplyFlag) {
            for (CommonTeamApplyData applyData : team.getApplyDataMap().values()) {
                builder.addApply(buildCommonTeamApplyData(applyData));
            }
        }
        return builder;
    }

    /**
     * 构建 CommonTeamUserData
     * @param userData
     * @return
     */
    public static CommonTeamProto.CommonTeamUserData.Builder buildCommonTeamUserData(CommonTeamUserData userData) {
        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(userData.getUserId());
        CommonTeamProto.CommonTeamUserData.Builder builder = CommonTeamProto.CommonTeamUserData.newBuilder();
        builder.setActivityId(userData.getActivityId());
        builder.setTeamId(userData.getTeamId());
        builder.setUserId(userData.getUserId());
        builder.setUserName(userData.getUserName());
        builder.setServerId(userData.getServerId());
        builder.setScore(userData.getScore());
        builder.setLastCreateTeamTime(userData.getLastCreateTeamTime());
        builder.setSelfLeaveTeamTime(userData.getSelfLeaveTeamTime());
        builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(crossUserBaseInfo.getUserBaseInfo()));
        List<Long> activityScoreList = CommonTeamMgr.getUserActivityScoreList(userData.getActivityId(), userData.getGroupId(), userData.getUserId());
        if (activityScoreList != null) {
            builder.addAllActivityScore(activityScoreList);
        }
        builder.setLastJoinTeamTime(userData.getLastJoinTeamTime());
        builder.setExtendsParam(userData.getExtendsParam());
        return builder;
    }

    /**
     * 构建 CommonTeamApplyData
     * @param applyData
     * @return
     */
    public static CommonTeamProto.CommonTeamApplyData.Builder buildCommonTeamApplyData(CommonTeamApplyData applyData) {
        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(applyData.getUserId());
        CommonTeamProto.CommonTeamApplyData.Builder builder = CommonTeamProto.CommonTeamApplyData.newBuilder();
        builder.setActivityId(applyData.getActivityId());
        builder.setTeamId(applyData.getTeamId());
        builder.setUserId(applyData.getUserId());
        builder.setUserName(applyData.getUserName());
        builder.setServerId(applyData.getServerId());
        builder.setScore(applyData.getScore());
        builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(crossUserBaseInfo.getUserBaseInfo()));
        builder.setApplyTime(applyData.getCreateTime());
        return builder;
    }

    /**
     * build TeamRankTempMsg
     * @param team
     * @return
     */
    public static CrossActivityProto.TeamRankTempMsg.Builder buildTeamRankTempMsg(CommonTeam team, int conditionType) {
        CrossActivityProto.TeamRankTempMsg.Builder tempMsg = CrossActivityProto.TeamRankTempMsg.newBuilder();
        CrossActivityProto.TeamBaseTempMsg.Builder builder = CrossActivityProto.TeamBaseTempMsg.newBuilder();
        builder.setTeamName(team.getTeamName());
        tempMsg.setTeamId(team.getTeamId());
        CrossUserBaseInfo leaderUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(team.getLeaderId());
        if (leaderUserBaseInfo != null) {
            PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(leaderUserBaseInfo.getUserBaseInfo());
            builder.setPlayerBaseData(baseMsg);
        }
        builder.setCaptainId(team.getLeaderId());
        tempMsg.setValue(String.valueOf(team.getRankScore(conditionType)));
        tempMsg.setParam(String.valueOf(team.getRankScoreParam(conditionType)));
        tempMsg.setTeamBaseData(builder);
        return tempMsg;
    }

}
